﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class IntegralDetails
    {
        public int Add(KeRui.Model.IntegralDetails model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Id", SqlDbType.Int, 4), new SqlParameter("@UserId", SqlDbType.Int, 4), new SqlParameter("@UserName", SqlDbType.NVarChar, 50), new SqlParameter("@Type", SqlDbType.Int, 4), new SqlParameter("@Integrals", SqlDbType.Int, 4), new SqlParameter("@HandleDate", SqlDbType.DateTime), new SqlParameter("@Remark", SqlDbType.NVarChar, 100) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.UserId;
            parameters[2].Value = model.UserName;
            parameters[3].Value = model.Type;
            parameters[4].Value = model.Integrals;
            parameters[5].Value = model.HandleDate;
            parameters[6].Value = model.Remark;
            DbHelperSQL.RunProcedure("IntegralDetails_ADD", parameters, out num);
            return (int) parameters[0].Value;
        }

        public void Delete(int Id)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Id", SqlDbType.Int, 4) };
            parameters[0].Value = Id;
            DbHelperSQL.RunProcedure("IntegralDetails_Delete", parameters, out num);
        }

        public KeRui.Model.IntegralDetails GetModel(int Id)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Id", SqlDbType.Int, 4) };
            parameters[0].Value = Id;
            KeRui.Model.IntegralDetails details = new KeRui.Model.IntegralDetails();
            DataSet set = DbHelperSQL.RunProcedure("IntegralDetails_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    details.Id = int.Parse(set.Tables[0].Rows[0]["Id"].ToString());
                }
                if (set.Tables[0].Rows[0]["UserId"].ToString() != "")
                {
                    details.UserId = int.Parse(set.Tables[0].Rows[0]["UserId"].ToString());
                }
                details.UserName = set.Tables[0].Rows[0]["UserName"].ToString();
                if (set.Tables[0].Rows[0]["Type"].ToString() != "")
                {
                    details.Type = int.Parse(set.Tables[0].Rows[0]["Type"].ToString());
                }
                if (set.Tables[0].Rows[0]["Integrals"].ToString() != "")
                {
                    details.Integrals = int.Parse(set.Tables[0].Rows[0]["Integrals"].ToString());
                }
                if (set.Tables[0].Rows[0]["HandleDate"].ToString() != "")
                {
                    details.HandleDate = DateTime.Parse(set.Tables[0].Rows[0]["HandleDate"].ToString());
                }
                details.Remark = set.Tables[0].Rows[0]["Remark"].ToString();
                return details;
            }
            return null;
        }

        public void Update(KeRui.Model.IntegralDetails model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Id", SqlDbType.Int, 4), new SqlParameter("@UserId", SqlDbType.Int, 4), new SqlParameter("@UserName", SqlDbType.NVarChar, 50), new SqlParameter("@Type", SqlDbType.Int, 4), new SqlParameter("@Integrals", SqlDbType.Int, 4), new SqlParameter("@HandleDate", SqlDbType.DateTime), new SqlParameter("@Remark", SqlDbType.NVarChar, 100) };
            parameters[0].Value = model.Id;
            parameters[1].Value = model.UserId;
            parameters[2].Value = model.UserName;
            parameters[3].Value = model.Type;
            parameters[4].Value = model.Integrals;
            parameters[5].Value = model.HandleDate;
            parameters[6].Value = model.Remark;
            DbHelperSQL.RunProcedure("IntegralDetails_Update", parameters, out num);
        }
    }
}

